<?php
/**
 * Created by PhpStorm.
 * User: 97606
 * Date: 2019/1/22
 * Time: 15:02
 */
namespace App\Http\Controllers\NewAdmin;

use App\Http\Controllers\AdapterWebController;
use Illuminate\Http\Request;
use App\Modules\User\User as ApiUser;
use App\Modules\Role\Role as ApiRole;
use App\Modules\Auth\Auth as ApiAuth;

class User extends AdapterWebController
{
    // 用户列表
    public function list(Request $request)
    {
        if (empty($request['keywords'])) {
            $postData['keywords'] = '';
        } else {
            $postData['keywords'] = $request['keywords'];
        }

        $list = $this->appLayerExec(ApiUser::class, 'list', $postData);
        return view('newAdmin.user.user_list', ['list' => $list['res']]);
    }

    // 修改用户密码
    public function password(Request $request)
    {
        return view('newAdmin.user.edit_password', ['id' => $request['id']]);
    }

    // 保存密码
    public function store(Request $request, $id)
    {
        if ($id < 1001) {
            return ['status' => -1, 'msg' => '无权修改管理员密码!'];
        }
        $postData['id'] = $id;
        $postData['login_password'] = $request['password'];
        $res = $this->appLayerExec(ApiUser::class, 'resetUserLoginPassword', $postData);
        if ($res['code'] == 200) {
            return ['status' => 0, 'msg' => '密码修改成功!'];
        }
        return ['status' => -1, 'msg' => '密码修改失败，请重试!'];
    }

    // 角色管理列表
    public function role()
    {
        $roles = $this->appLayerExec(ApiRole::class, 'list');
        return view('newAdmin.user.role_list', ['roles' => $roles['res']]);
    }

    // 添加角色
    public function addRole()
    {
        $postData['name'] = \request('name');
        $postData['name_cn'] = \request('name_cn');
        $res = $this->appLayerExec(ApiRole::class, 'add', $postData);
        if ($res['code'] == 200) {
            return ['status' => 0, 'msg' => '角色添加成功!'];
        }
        return ['status' => -1, 'msg' => '角色添加成功，请重试!'];
    }

    // 启用角色
    public function roleEnable($id)
    {
        $postData['id'] = (int)$id;
        $res = $this->appLayerExec(ApiRole::class, 'start', $postData);
        if ($res['code'] == 200) {
            return ['status' => 0, 'msg' => '角色已启用!'];
        }
        return ['status' => -1, 'msg' => '角色启用异常，请重试!'];
    }

    // 禁用角色
    public function roleDisable($id)
    {
        $postData['id'] = (int)$id;
        $res = $this->appLayerExec(ApiRole::class, 'stop', $postData);
        if ($res['code'] == 200) {
            return ['status' => 0, 'msg' => '角色已禁用!'];
        }
        return ['status' => -1, 'msg' => '角色禁用异常，请重试!'];
    }

    // 权限管理列表
    public function auth()
    {
        if (empty(\request('keywords'))) {
            $postData['keywords'] = '';
        } else {
            $postData['keywords'] = \request('keywords');
        }

        $auths = $this->appLayerExec(ApiAuth::class, 'list', $postData);
        return view('newAdmin.user.auth_list', ['auths' => $auths['res'], 'keywords' => $postData['keywords']]);
    }

    // 编辑权限
    public function editAuth($id)
    {
        if (\request()->isMethod('get')) {
            $postData['id'] = $id;
            $res = $this->appLayerExec(ApiAuth::class, 'authInfo', $postData);
            return view('newAdmin.user.edit_auth', ['auth' => $res['res']]);
        }
        $postData['id'] = $id;
        $postData['other_check'] = '{}';
        $postData['name'] = \request('name');
        $postData['server'] = \request('server');
        $postData['module'] = \request('module');
        $postData['controller'] = \request('controller');
        $postData['action'] = \request('action');
        $res = $this->appLayerExec(ApiAuth::class, 'edit', $postData);
        if ($res['code'] == 200) {
            return ['status' => 0, 'msg' => '权限修改成功!'];
        }
        return ['status' => -1, 'msg' => '权限修改异常，请重试!'];
    }

    // 增加权限/操作
    public function storeAuth()
    {
        if (\request()->isMethod('get')) {
            return view('newAdmin.user.store_auth');
        }
        $postData['name'] = \request('name');
        $postData['server'] = \request('server');
        $postData['module'] = \request('module');
        $postData['controller'] = \request('controller');
        $postData['action'] = \request('action');
        $postData['other_check'] = '';
        $postData['app_name'] = 11;
        $res = $this->appLayerExec(ApiAuth::class, 'add', $postData);
        if ($res['code'] == 200) {
            return ['status' => 0, 'msg' => '权限保存成功!'];
        }
        return ['status' => -1, 'msg' => '权限保存异常，请重试!'];
    }

    // 权限赋予角色页面
    public function give($id)
    {
        $postData['auth_id'] = $id;
        $roles = $this->appLayerExec(ApiAuth::class, 'roleByAuth', $postData);
        return view('newAdmin.user.give_auth', ['id' => $id, 'roles' => $roles['res']]);
    }

    // 赋予角色某权限
    public function authGiveRole()
    {
        $postData['auth_id'] = \request('auth_id');
        $postData['role_id'] = \request('role_id');
        $res = $this->appLayerExec(ApiAuth::class, 'addAuthByRole', $postData);
        if ($res['code'] == 200) {
            return ['status' => 0, 'msg' => '操作成功!'];
        }
        return ['status' => -1, 'msg' => '操作异常，请重试!'];
    }

    // 解除角色某权限
    public function autheRmoveRole()
    {
        $postData['auth_ids'] = \request('auth_id');
        $postData['role_id'] = \request('role_id');
        $res = $this->appLayerExec(ApiAuth::class, 'removeAuthsByRole', $postData);
        if ($res['code'] == 200) {
            return ['status' => 0, 'msg' => '操作成功!'];
        }
        return ['status' => -1, 'msg' => '操作异常，请重试!'];
    }

    // 实名认证(司机/企业)
    public function ident($type)
    {
        if ($type == 'driver') {
            if (empty(request('keywords'))) {
                $postData['keywords'] = '';
            } else {
                $postData['keywords'] = request('keywords');
            }
            $postData['status'] = 0;
            $list = $this->appLayerExec(ApiUser::class, 'getAuthticationList', $postData);
            return view('newAdmin.user.ident_driver', ['list' => $list['res'], 'type' => 'driver']);
        }
        if ($type == 'enterprise') {
            if (empty(request('keywords'))) {
                $postData['keywords'] = '';
            } else {
                $postData['keywords'] = request('keywords');
            }
            $postData['status'] = 0;
            $list = $this->appLayerExec(ApiUser::class, 'getEnterpriseInfo', $postData);
            return view('newAdmin.user.ident_enterprise', ['list' => $list['res'], 'type' => 'enterprise']);
        }
        return false;
    }

    // 审核实名认证(司机/企业)
    public function auditStatus()
    {
        $postData['id'] = \request('id');
        $postData['status'] = \request('status');
        $postData['type'] = \request('type');
        $res = $this->appLayerExec(ApiUser::class, 'verifyAuthentication', $postData);
        if ($res['code'] == 200) {
            return ['status' => 0, 'msg' => '操作成功!'];
        }
        return ['status' => -1, 'msg' => '操作异常，请重试!'];
    }
}
